// axios 二次封装
import axios from "axios";
import Qs from "qs";

axios.defaults.baseURL = "http://localhost:3000";

function testGet(url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: "get",
      url,
      params
    })
      .then(res => {
        console.log(res);
        resolve(res);
      })
      .catch(err => {
        console.log(err);
        reject(err);
      });
  });
}

function testPost(url, data) {
  return new Promise((resolve, reject) => {
    axios({
      method: "post",
      url,
      data
    })
      .then(res => {
        console.log(res);
        resolve(res);
      })
      .catch(err => {
        console.log(err);
        reject(err);
      });
  });
}
function testFormPost(url, data) {
  return new Promise((resolve, reject) => {
    axios({
      method: "post",
      url,
      data,
      transformRequest: [
        function(data) {
          let ret = "";
          ret = Qs.stringify(data);
          return ret;
        }
      ],
      headers: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      }
    })
      .then(res => {
        console.log(res);
        resolve(res);
      })
      .catch(err => {
        console.log(err);
        reject(err);
      });
  });
}
export { testGet, testPost, testFormPost };
